package org.arithmetic.string;

import java.util.Scanner;

/**
 * Created by LUOLIANG on 2016/8/31.
 * 求query和text两字符串的最大公共子串
 */
public class LongestSubStr {
    public static int getLong(String query, String text) {

        for (int i = text.length(); i >= 0; i--) {
            for (int j = 0; j < text.length(); j++) {
                if (i + j <= text.length()) {
                    String temp = text.substring(j, i + j);
                    if (query.contains(temp)) {
                        System.out.println("最长公共子串为：" + temp);
                        System.out.println("长度为：" + temp.length());
                        return temp.length();
                    }
                }
            }
        }

        return 0;
    }

    public static void main(String[] args) {
        System.out.println("请输入两字符串，以空格隔开：");
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String[] strs = scanner.nextLine().split(" ");
            System.out.println(strs[0]);
            System.out.println(strs[1]);

            getLong(strs[0], strs[1]);
        }
    }
}
